<?php

class UserController extends ControllerBase {

    //用户列表页面
    public function indexAction() {
        $numberPage = empty($_GET['page']) ? 1 : $_GET['page'] * 1;
        $data       = User::find(['order' => 'id desc']);
        // var_dump($data);exit;
        $paginator        = new Phalcon\Paginator\Adapter\Model([
            'data'  => $data,
            'limit' => 3,
            'page'  => $numberPage,
        ]);
        $this->view->page = $paginator->getPaginate();
    }

    //用户注册表单页面
    public function registerAction() {

        //post提交数据
        if($this->request->isPost()) {
            $form = new UserForm();
            //实例化model类
            $user = new User();
            $user->validationHasFailed();
            $data    = $this->request->getPost();
            $success = $user->save($this->request->getPost(), ['username', 'password']);

            if($form->isValid($data) === false) {
                foreach($form->getMessages() as $message) {
                    $this->flash->error($message);
                }
                $this->dispatcher->forward([
                    'controller' => "user",
                    'action'     => 'index'
                ]);
                return;
            }
            if($success) {
                //接收参数
                $user->username = $this->request->getPost("username");
                $user->password = $this->request->getPost("password");
                if($user->save()) {
                    // echo "添加成功";
                    $this->flash->success("添加成功");

                    $this->dispatcher->forward([
                        'controller' => "user",
                        'action'     => 'index'
                    ]);
                }
            } else {
                echo "添加失败";
            }
        } //默认加载视图
        else {
        }
    }

    /**
     * @return \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface
     *
     * 查询数据已有的用户名
     */
    public function checkAction() {
        $username = $this->request->get('username');
        $user     = new User();
        //        $re=$user::find(array("username='aaaa'"));
        //        $re = $re->toArray();
        if($user::find(["username = '$username'"])->toArray()) {
            return $this->response->setJsonContent(false);
        } else {
            return $this->response->setJsonContent(true);
        }
    }

    //删除用户
    public function deleteAction($id) {
        //先查询该用户
        $admin = User::findFirstByid($id);
        if(!$admin) {
            $this->flash->error("此用户不存在");

            $this->dispatcher->forward([
                'controller' => "user",
                'action'     => 'index'
            ]);
            return;
        }
        //删除失败获取错误信息
        if(!$admin->delete()) {
            foreach($admin->getMessages() as $message) {
                $this->flash->error($message);
            }
            $this->dispatcher->forward([
                'controller' => "user",
                'action'     => 'index'
            ]);
            return;
        }
        $this->flash->success("删除成功");
        $this->dispatcher->forward([
            'controller' => "user",
            'action'     => "index"
        ]);
    }

    //修改用户
    public function editAction($id) {

        if(!$this->request->isPost()) {
            $user = User::findFirstByid($id);
            if(!$user) {
                $this->flash->error("此用户不存在");
                $this->dispatcher->forward([
                    'controller' => 'user',
                    'action'     => 'index',
                ]);
                return;
            }
            $this->view->id = $user->id;
            $this->tag->setDefault('id', $user->id);
            $this->tag->setDefault('username', $user->username);
            $this->tag->setDefault('password', $user->password);
        }
    }

    /**
     * 修改
     */
    public function saveAction() {
        if(!$this->request->isPost()) {
            $this->dispatcher->forward([
                'controller' => 'user',
                'action'     => 'index'
            ]);

            return;
        }
        $id   = $this->request->getPost("id");
        $user = User::findFirstByid($id);
        if($this->request->isPost()) {
            if(!$user) {
                $this->flash->error("此用户不存在" . $id);
                $this->dispatcher->forward([
                    'controller' => 'user',
                    'action'     => 'index',
                ]);
                return;
            }
        }
        $user->username = $this->request->getPost('username');
        $user->password = $this->request->getPost('password');
        if(!$user->save()) {
            foreach($user->getMessages() as $message) {
                $this->flash->error($message);
            }
            $this->dispatcher->forward([
                'controller' => "user",
                'action'     => 'index',
            ]);
            return;
        }
        $this->flash->success("修改成功");
        $this->dispatcher->forward([
            'controller' => "user",
            'action'     => 'index'
        ]);
    }
}

